package Thread.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 使用Callable接口实现
 */
public class Callabletest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum=0;
                for (int i = 0; i <=1000 ; i++) {
                    sum+=i;
                }
                return sum;
            }
        };
        //接收Call方法的返回值使用FutuerTask类
        FutureTask<Integer> futureTask=new FutureTask<>(callable);
        //Thread类接收Callable接口必须通过FutuerTask类
        Thread t=new Thread(futureTask);
        t.start();
        //get方法会阻塞当前线程，直到call方法执行完毕，才会恢复当前线程
        int result=futureTask.get();
        System.out.println("子线程结束，result ="+result);
    }
}
